home *** CD-ROM | disk | FTP | other *** search
- #ifndef __STDFILE__
- #define __STDFILE__ 1
-
- #ifndef __TYPES__
- #include <types.h>
- #endif
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- #ifndef __STDLIB__
- #include <stdlib.h>
- #endif
-
-
- // Define our segment for outlined inlines
- #pragma segment StdFile
-
-
- class StdFile : public SingleObject {
- public:
- StdFile();
- StdFile(const FILE *f);
- StdFile(const char *fileName, const char *mode);
- StdFile(const StdFile &aStdFile);
-
- virtual ~StdFile();
-
-
- FILE *Open(const char *fileName, const char *mode);
- // Open the StdFile independent of other operations
-
- void Close();
- // Close the StdFile
-
- operator FILE *() const;
- // Get the FILE* (as for fscanf)
-
-
- // Formatted output operations
- inline int Putc(int c) const;
- inline int Puts(const char *str) const;
- inline int Print(const char *str) const;
- inline int Print(int v) const;
- inline int Print(extended v) const;
- inline int Printf(const char *fmt, ...) const;
-
- // Unformatted output operations
- inline size_t Write(const void *p, size_t n) const;
- inline size_t Write(const void *p, size_t s, size_t n) const;
-
- void Flush() const;
-
- // Formatted input operations
- char *Gets(char *s, int n) const;
- int Getc() const;
- int Get(unsigned char &v) const;
- int Get(unsigned short &v) const;
- int Get(unsigned int &v) const;
- int Get(unsigned long &v) const;
- int Get(char &v) const;
- int Get(short &v) const;
- int Get(int &v) const;
- int Get(long &v) const;
- int Get(float &v) const;
- int Get(double &v) const;
- int Get(extended &v) const;
- int Ungetc(int c) const;
-
- // Unformatted input operations
- size_t Read(void *p, size_t n) const;
- size_t Read(void *p, size_t s, size_t n) const;
-
-
- // Seek operations
- int Seek(long offset, int origin = SEEK_SET) const;
- void Rewind() const;
-
- int GetPos(fpos_t *p) const;
- int SetPos(fpos_t *p) const;
-
- size_t GetSize() const;
- /*
- ** Return length of the file. Returns 0 on error or if the file
- ** is a standard input file which is not connected to a real
- ** disk file
- */
-
-
- // Error operations
- void ClearErr() const;
- Boolean IsEOF() const;
- int Error() const;
-
-
- private:
- Boolean fIsCopy; // True if constructed from a StdFile
- FILE *fFile;
-
- private:
- static const char sOIFmt[]; // Output integer format
- static const char sOEFmt[]; // Output floating point format
-
- static const char sIUCFmt[]; // Input unsigned char format...
- static const char sIUSFmt[];
- static const char sIUIFmt[];
- static const char sIULFmt[];
- static const char sICFmt[];
- static const char sISFmt[];
- static const char sIIFmt[];
- static const char sILFmt[];
- static const char sIFFmt[];
- static const char sIDFmt[];
- static const char sIEFmt[];
- };
-
-
- // Standard file descriptors
- extern StdFile gStdin;
- extern StdFile gStdout;
- extern StdFile gStderr;
-
-
- inline StdFile::StdFile()
- : fFile(0),
- fIsCopy(false)
- {
- }
-
-
- inline StdFile::StdFile(const FILE *f)
- : fFile((FILE *)f),
- fIsCopy(false)
- {
- }
-
-
- inline StdFile::StdFile(const char *fileName, const char *mode)
- : fFile(fopen(fileName, mode)),
- fIsCopy(false)
- {
- }
-
-
- inline StdFile::StdFile(const StdFile &aStdFile)
- : fFile(aStdFile.fFile),
- fIsCopy(true)
- {
- }
-
-
- inline FILE *StdFile::Open(const char *fileName, const char *mode)
- {
- return (fFile = fopen(fileName, mode));
- }
-
-
- inline StdFile::operator FILE *() const
- {
- return (fFile);
- }
-
-
- inline int StdFile::Putc(int c) const
- {
- return (putc(c, fFile));
- }
-
-
- inline int StdFile::Print(const char *str) const
- {
- return (fputs(str, fFile));
- }
-
-
- inline int StdFile::Puts(const char *str) const
- {
- Print(str);
- return (Putc('\n'));
- }
-
-
- inline int StdFile::Print(int v) const
- {
- return (Printf(sOIFmt, v));
- }
-
-
- inline int StdFile::Print(extended v) const
- {
- return (Printf(sOEFmt, v));
- }
-
-
- inline size_t StdFile::Write(const void *p, size_t s, size_t n) const
- {
- return (fwrite(p, s, n, fFile));
- }
-
-
- inline size_t StdFile::Write(const void *p, size_t n) const
- {
- return (Write(p, sizeof(char), n));
- }
-
-
- inline void StdFile::Flush() const
- {
- fflush(fFile);
- }
-
-
- inline char *StdFile::Gets(char *s, int n) const
- {
- return (fgets(s, n, fFile));
- }
-
-
- inline int StdFile::Getc() const
- {
- return (getc(fFile));
- }
-
-
- inline int StdFile::Ungetc(int c) const
- {
- return (ungetc(c, fFile));
- }
-
-
- inline size_t StdFile::Read(void *p, size_t s, size_t n) const
- {
- return (fread(p, s, n, fFile));
- }
-
-
- inline size_t StdFile::Read(void *p, size_t n) const
- {
- return (Read(p, sizeof(char), n));
- }
-
-
- inline int StdFile::Seek(long offset, int origin) const
- {
- return (fseek(fFile, offset, origin));
- }
-
-
- inline void StdFile::Rewind() const
- {
- rewind(fFile);
- }
-
-
- inline int StdFile::GetPos(fpos_t *p) const
- {
- return (fgetpos(fFile, p));
- }
-
-
- inline int StdFile::SetPos(fpos_t *p) const
- {
- return (fsetpos(fFile, p));
- }
-
-
- inline void StdFile::ClearErr() const
- {
- clearerr(fFile);
- }
-
-
- inline Boolean StdFile::IsEOF() const
- {
- return (feof(fFile));
- }
-
-
- inline int StdFile::Error() const
- {
- return (ferror(fFile));
- }
-
-
- #endif
-
-
-